//
//  MainTabBarController.m
//  drinkApp
//
//  Created by cnbizMedia on 14/8/11.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//

#import "MainTabBarController.h"
#import "AppDelegate.h"
//#import "UIViewController+MMDrawerController.h"
#import "UITabBarItem+CustomBadge.h"
#import "TableViewController.h"
#define SelectedColor [UIColor redColor]
#define UnselectedColor UIColorFromRGB(0x888888)

@interface MainTabBarController (){
//    UILabel*label;

    NSArray *img;
    NSArray *img_selected;
    UITabBarItem*meVc;
    
    UIImageView *it1Imgb;
    UIImageView *it2Imgb;
    UIImageView *it3Imgb;
    UIImageView *it4Imgb;
    
    
    UIImageView *bottomImage1 ;
    UIImageView *bottomImage2 ;
    UIImageView *bottomImage3 ;
    UIImageView *bottomImage4;
    
    NSData *bottomImageData1  ;
    NSData *bottomImageData2  ;
    NSData *bottomImageData3  ;
    NSData *bottomImageData4  ;

}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *rightBarButton;

@end

@implementation MainTabBarController

- (BOOL)loginCheck{
    //NSLog(@"TOKEN:%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"token"]);
    NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:@"token"];
    if(token == nil || [token isKindOfClass:[NSNull class]] || [token length] < 1){
        return NO;
    }
    return YES;
}
-(void)viewWillAppear:(BOOL)animated{
//    AppDelegate*app=[UIApplication sharedApplication].delegate;
//    BadgeView.hidden=YES;
//    [app.BadgeArray addObject:BadgeView];
//    [self setRightBarButton];

}


-(void)setRightBarButton{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"user"]) {
        NSString *url = [NSString stringWithFormat:@"http://ksjv2.kanshangjie.com/Statics/UserHead?uid=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user"][@"user_id"]];
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            [button setImage:image forState:UIControlStateNormal];
            [button setFrame:CGRectMake(0, 0, 25, 25)];
            [button addTarget:self action:@selector(rightBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            ;
            [button.layer setCornerRadius:(button.frame.size.height / 2)];
            [button.layer setMasksToBounds:YES];
            UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
            self.navigationItem.rightBarButtonItem= rightItem;
        }];
        return;
    }
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"rb"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    [button addTarget:self action:@selector(rightBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem= rightItem;
    
    
    

}
- (IBAction)rightBarButtonClicked:(id)sender{
//    if([self loginCheck]){
//        登录了进设置页面
        [self performSegueWithIdentifier:@"setting" sender:self];
//        [self performSegueWithIdentifier:@"profilePage" sender:self];
//    }else{
        //没登录，进登录页面
//        [self performSegueWithIdentifier:@"loginPage" sender:self];
//    }
}


- (void)saveImageToPhotos:(UIImage*)savedImage

{
   
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
}

// 指定回调方法

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{
    
    NSString *msg = nil ;
    
    if(error != NULL){
        
        msg = @"保存图片失败" ;
        
    }else{
        
        msg = @"保存图片成功" ;
        
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                         
                                                    message:msg
                      
                                                   delegate:self
                          
                                          cancelButtonTitle:@"确定"
                          
                                          otherButtonTitles:nil];
    
    [alert show];
    
}







//从服务器获取新春版UI
- (void)getTheImageFromTheWeb {

    [[AppAPIClient sharedClient] GET:@"Theme/Current" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *result = responseObject[@"data"];
        NSString *theme = result[@"theme"];
        
//        NSString *themeOfTheImage = [UserDefaults objectForKey:@"themeOfTheImage"];
        
        NSLog(@"headBack_NewYear5-6-6p = %@ %@ %@",result[@"top"][@"top_1x"],result[@"top"][@"top_2x"],result[@"top"][@"top_3x"]);
        NSLog(@"bottomBtm1-2-3-4 = %@ %@ %@ %@",result[@"bottom"][@"bottom_1_3x"],result[@"bottom"][@"bottom_2_3x"],result[@"bottom"][@"bottom_3_3x"],result[@"bottom"][@"bottom_4_3x"]);
        
        [UserDefaults setObject:result[@"top"][@"top_1x"] forKey:@"headBack_NewYear5"];
        [UserDefaults setObject:result[@"top"][@"top_2x"] forKey:@"headBack_NewYear6"];
        [UserDefaults setObject:result[@"top"][@"top_3x"] forKey:@"headBack_NewYear6p"];
        
        
        [UserDefaults setObject:result[@"bottom"][@"bottom_1_3x"] forKey:@"bottomBtm1"];
        [UserDefaults setObject:result[@"bottom"][@"bottom_2_3x"] forKey:@"bottomBtm2"];
        [UserDefaults setObject:result[@"bottom"][@"bottom_3_3x"] forKey:@"bottomBtm3"];
        [UserDefaults setObject:result[@"bottom"][@"bottom_4_3x"] forKey:@"bottomBtm4"];


        [UserDefaults setObject:result[@"topuser"][@"top_3x"] forKey:@"topuser"];
        [UserDefaults setObject:result[@"topback"][@"top_3x"] forKey:@"topback"];
        [UserDefaults setObject:result[@"topadd"][@"top_3x"] forKey:@"topadd"];
        [UserDefaults setObject:result[@"topserch"][@"top_3x"] forKey:@"topserch"];
        
        
        [UserDefaults setObject:result[@"fontcolor1"]  forKey:@"fontcolor1"];
        [UserDefaults setObject:result[@"fontcolor2"]  forKey:@"fontcolor2"];
        [UserDefaults setObject:result[@"fontcolor3"]  forKey:@"fontcolor3"];
     
        
        [bottomImage1 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_1_3x"]]];
        [bottomImage2 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_2_3x"]]];
        [bottomImage3 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_3_3x"]]];
        [bottomImage4 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_4_3x"]]];
        

        
        
//        if (themeOfTheImage.length < 1) {
//             themeOfTheImage = @"0";
//        }
        
//        if ([theme isEqualToString:themeOfTheImage]) {
//            
//        }
//        else{
//        [UserDefaults setValue:theme forKey:@"themeOfTheImage"];
//            
//            
////           z for (int i =0; i<self.tabBar.items.count; i++) {
////                UITabBarItem*it=self.tabBar.items[i];
////                UIOffset offset;
////                
////                offset.horizontal = 0.0;
////                
////                offset.vertical = -5.0;
////                
////                [it setTitlePositionAdjustment:offset];
////                
////                
////                //        [it setFinishedSelectedImage:[UIImage imageNamed:img_selected[i]] withFinishedUnselectedImage:[UIImage imageNamed:img[i]]];
////                it.image =  [[UIImage imageNamed:img[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
////                it.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@",img_selected[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
////                
////                if (i==0) {
////                    
////                    [it setTitleTextAttributes:@{ NSForegroundColorAttributeName : SelectedColor } forState:UIControlStateNormal];
////                    
////                    
////                }
////                if (i==self.tabBar.items.count-1) {
////                    
////                    AppDelegate*app= APPDelegate;
////                    [it setMyAppCustomBadgeValue:app.BadgeStr];
////                    [app.BadgeArray addObject:it];
////                }
////                
////            }
//
//        }
//        
        
        
//
//        [topImage1 sd_setImageWithURL:[NSURL URLWithString:result[@"top"][@"top_1x"]]];
//        [topImage2 sd_setImageWithURL:[NSURL URLWithString:result[@"top"][@"top_2x"]]];
//        [topImage3 sd_setImageWithURL:[NSURL URLWithString:result[@"top"][@"top_3x"]]];
//
        
        
   
//
//        //
//
//        
//       //
//        NSMutableArray *topImgAry = [[NSMutableArray alloc] init];
//        topImgAry

        //获取到bar按钮图
//        UIImageView *bottomImage1 = [[UIImageView alloc] init];
//        UIImageView *bottomImage2 = [[UIImageView alloc] init];
//        UIImageView *bottomImage3 = [[UIImageView alloc] init];
//        UIImageView *bottomImage4 = [[UIImageView alloc] init];
        
        //获取到头图
        UIImageView *topImage1 = [[UIImageView alloc] init];
        UIImageView *topImage2 = [[UIImageView alloc] init];
        UIImageView *topImage3 = [[UIImageView alloc] init];
        
        [topImage1 sd_setImageWithURL:[NSURL URLWithString:result[@"top"][@"top_1x"]]];
        [topImage2 sd_setImageWithURL:[NSURL URLWithString:result[@"top"][@"top_2x"]]];
        [topImage3 sd_setImageWithURL:[NSURL URLWithString:result[@"top"][@"top_3x"]]];


        NSData *topImageData1 = UIImagePNGRepresentation(topImage1.image) ;
        NSData *topImageData2 = UIImagePNGRepresentation(topImage2.image) ;
        NSData *topImageData3 = UIImagePNGRepresentation(topImage3.image) ;
        
//        NSLog(@"topImageData1 == %@   2  %@   3   %@",topImageData1,topImageData2,topImageData3);
        
        [UserDefaults setObject:topImageData1 forKey:@"topImage5SetObject"];
        [UserDefaults setObject:topImageData2 forKey:@"topImage6SetObject"];
        [UserDefaults setObject:topImageData3 forKey:@"topImage6PSetObject"];
        
        UIImageView *bbtImage1 = [[UIImageView alloc]init];
        UIImageView *bbtImage2 = [[UIImageView alloc]init];
        UIImageView *bbtImage3 = [[UIImageView alloc]init];
        UIImageView *bbtImage4 = [[UIImageView alloc]init];
        
        [bbtImage1 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_1_2x"]] placeholderImage:[UIImage imageNamed:@"going"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"图片加载完成后做的事情");
            bottomImageData1 = UIImagePNGRepresentation(bbtImage1.image) ;
            [self setTheTabBerItem];
            
            [self saveImageToPhotos:image];
        }];
        
        [bbtImage2 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_2_1x"]] placeholderImage:[UIImage imageNamed:@"going"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"图片加载完成后做的事情");
            bottomImageData2 = UIImagePNGRepresentation(bbtImage2.image) ;
            [self setTheTabBerItem];
            [self saveImageToPhotos:image];
        }];
        
        [bbtImage3 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_3_2x"]] placeholderImage:[UIImage imageNamed:@"going"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"图片加载完成后做的事情");
            bottomImageData3 = UIImagePNGRepresentation(bbtImage3.image) ;
            [self setTheTabBerItem];
            [self saveImageToPhotos:image];
        }];
        
        [bbtImage4 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_4_3x"]] placeholderImage:[UIImage imageNamed:@"going"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"图片加载完成后做的事情");
            bottomImageData4 = UIImagePNGRepresentation(bbtImage4.image) ;
            [self setTheTabBerItem];
            [self saveImageToPhotos:image];
        }];
        
//        [bbtImage1 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_1_3x"]]];
//        [bbtImage2 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_2_3x"]]];
//        [bbtImage3 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_3_3x"]]];
//        [bbtImage4 sd_setImageWithURL:[NSURL URLWithString:result[@"bottom"][@"bottom_4_3x"]]];
        
//        bottomImageData1 = UIImagePNGRepresentation(bbtImage1.image) ;
//        bottomImageData2 = UIImagePNGRepresentation(bbtImage2.image) ;
//        bottomImageData3 = UIImagePNGRepresentation(bbtImage3.image) ;
//        bottomImageData4 = UIImagePNGRepresentation(bbtImage4.image) ;

//        NSLog(@"bottomImageData1 == %@   2  %@   3   %@   4   %@",bottomImageData1,bottomImageData2,bottomImageData3,bottomImageData4);
        
        
        
        [UserDefaults setObject:bottomImageData1 forKey:@"bottomImageSetObject1"];
        [UserDefaults setObject:bottomImageData2 forKey:@"bottomImageSetObject2"];
        [UserDefaults setObject:bottomImageData3 forKey:@"bottomImageSetObject3"];
        [UserDefaults setObject:bottomImageData4 forKey:@"bottomImageSetObject4"];
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex:0];
        //        NSArray *array = [[NSArray alloc] initWithObjects:@"i love you",@"1234567",nil];
        NSString *testDirectory = [docDir stringByAppendingPathComponent:@"themePack"];
        //找到之前创建的文件夹目录
        
        
        
        //判断createPath路径文件夹是否已存在，此处createPath为需要新建的文件夹的绝对路径
        if ([[NSFileManager defaultManager] fileExistsAtPath:testDirectory])        {
//            return NO;
        }
        else
        {
            
            [[NSFileManager defaultManager] createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹
//            return YES;
        }
        
        NSString *bottomImageSandBox1 = [testDirectory stringByAppendingPathComponent:@"bottomImage1"];
        NSString *bottomImageSandBox2 = [testDirectory stringByAppendingPathComponent:@"bottomImage2"];
        NSString *bottomImageSandBox3 = [testDirectory stringByAppendingPathComponent:@"bottomImage3"];
        NSString *bottomImageSandBox4 = [testDirectory stringByAppendingPathComponent:@"bottomImage4"];

        NSString *topImageSandBox1 = [testDirectory stringByAppendingPathComponent:@"topImage1"];
        NSString *topImageSandBox2 = [testDirectory stringByAppendingPathComponent:@"topImage2"];
        NSString *topImageSandBox3 = [testDirectory stringByAppendingPathComponent:@"topImage3"];

        
        //在上面的文件夹目录创建这个文件的名字
        
        //        NSString *filePath = [docDir stringByAppendingPathComponent:imageName];
        //        [array writeToFile:filePath atomically:YES];
        
        [topImageData1 writeToFile:topImageSandBox1 atomically:YES];
        [topImageData2 writeToFile:topImageSandBox2 atomically:YES];
        [topImageData3 writeToFile:topImageSandBox3 atomically:YES];
        
        [bottomImageData1 writeToFile:bottomImageSandBox1 atomically:YES];
        [bottomImageData2 writeToFile:bottomImageSandBox2 atomically:YES];
        [bottomImageData3 writeToFile:bottomImageSandBox3 atomically:YES];
        [bottomImageData4 writeToFile:bottomImageSandBox4 atomically:YES];
        
        
        
        //通过这个文件名字，写入数据
        
        NSLog(@"在这里获取到照片的地址");
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ERR: %@",operation.responseString);
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.translucent=NO;
   
    
 
//    BadgeView.text=@"1";
//    [self.tabBar addSubview:BadgeView];
    
//    self.tabBar.barTintColor = UIColorFromRGB(0x333333);//[UIColor whiteColor];
//    self.tabBar.tintColor = UnselectedColor;
    
    img = @[@"sy",@"dy",@"Money_bag02",@"q"];                                            //,@"Discovery"
    img_selected =  @[@"sy_a",@"dy_a",@"Money_bag01",@"q_a"];                         //,@"Discovery_a"
//    [self getTheImageFromTheWeb];
    
    //现在的时间
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    NSLog(@"now time          == %llu",recordTime);
    
    
    //获取的时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd-HH-mm"];
    
    NSDate *date = [formatter dateFromString:@"2016-01-18-10-10"];
    UInt64 needTime = [date timeIntervalSince1970]*1000;
    NSLog(@"that day time     == %llu",needTime);

    NSDate *theEndTime = [formatter dateFromString:@"2016-02-25-00-10"];
    UInt64 endTime = [theEndTime timeIntervalSince1970]*1000;
    NSLog(@"that end day time == %llu",endTime);
//    [UserDefaults setValue:@"NotNewYear" forKey:@"NewYear"];
    
    if ( recordTime > needTime) {
        
        img = @[@"jingxuan_NewYear",@"Order_NewYear",@"clube_NewYear",@"ID_NewYear"];                                            //,@"Discovery"
        img_selected =  @[@"jingxuan_NewYear",@"Order_NewYear",@"clube_NewYear",@"ID_NewYear"];

        
        [UserDefaults setValue:@"NewYear" forKey:@"NewYear"];
        

    }
    
    if ( recordTime > endTime) {
        
        img = @[@"sy",@"dy",@"Money_bag02",@"q"];                                            //,@"Discovery"
        img_selected =  @[@"sy_a",@"dy_a",@"Money_bag01",@"q_a"];
        
         [UserDefaults setValue:@"NotNewYear" forKey:@"NewYear"];
    }
    
//    [self setRightBarButton];
    
    
  
    
    NSLog(@"bottomImageData1 == %@   2  %@   3   %@   4   %@",bottomImageData1,bottomImageData2,bottomImageData3,bottomImageData4);
    
//    UIImage *bottom1 = [UIImage imageWithData:bottomImageData1];
//    UIImage *bottom2 = [UIImage imageWithData:bottomImageData2];
//    UIImage *bottom3 = [UIImage imageWithData:bottomImageData3];
//    UIImage *bottom4 = [UIImage imageWithData:bottomImageData4];
    
//    NSLog(@"bottom1 = %@ =%@ =%@ =%@",bottom1,bottom2,bottom3,bottom4);
    
    //这是以前的方法
    for (int i =0; i<self.tabBar.items.count; i++) {
        UITabBarItem*it=self.tabBar.items[i];
        UIOffset offset;
        
        offset.horizontal = 0.0;
        
        offset.vertical = -6.0;
        
        [it setTitlePositionAdjustment:offset];
        
        

//        [it setFinishedSelectedImage:[UIImage imageNamed:img_selected[i]] withFinishedUnselectedImage:[UIImage imageNamed:img[i]]];
       
        
        

        if (i==0) {
            it.image =  [[UIImage imageNamed:img[0]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            it.selectedImage = [[UIImage imageNamed:img_selected[0]]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            [it setTitleTextAttributes:@{ NSForegroundColorAttributeName : SelectedColor } forState:UIControlStateNormal];
            
            
        }
        
        it.image =  [[UIImage imageNamed:img[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        it.selectedImage = [[UIImage imageNamed:img_selected[i]]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        if (i==self.tabBar.items.count-1) {
         
            
            AppDelegate*app= APPDelegate;
            [it setMyAppCustomBadgeValue:app.BadgeStr];
            [app.BadgeArray addObject:it];
        }
        
//        if (i==1) {
//            it.image =  [bottom2  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//            it.selectedImage = [bottom2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//            
//        }
//        
//        if (i==2) {
//            it.image =  [bottom3  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//            it.selectedImage = [bottom3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//            
//        }
//        
//        if (i==3) {
//            it.image =  [bottom4  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//            it.selectedImage = [bottom4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//            
//        }
    
    }
    

//    [self setTheTabBerItem];
    
    
    [[AppData sharedClient] initData];
    //[self.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"pic_10.png"]];
}

- (void)setTheTabBerItem{

    
    NSLog(@"bottomImageData1 == %@   2  %@   3   %@   4   %@",bottomImageData1,bottomImageData2,bottomImageData3,bottomImageData4);
    
    UIImage *bottom1 = [UIImage imageWithData:bottomImageData1];
    UIImage *bottom2 = [UIImage imageWithData:bottomImageData2];
    UIImage *bottom3 = [UIImage imageWithData:bottomImageData3];
    UIImage *bottom4 = [UIImage imageWithData:bottomImageData4];
    
    NSLog(@"bottom1 = %@ =%@ =%@ =%@",bottom1,bottom2,bottom3,bottom4);
    
    
    UITabBarItem*it1=self.tabBar.items[0];
    UIOffset offset;
    
    offset.horizontal = 0.0;
    
    offset.vertical = -6.0;
    
    [it1 setTitlePositionAdjustment:offset];
    
    UIImageView *it1Img     = [[UIImageView alloc]init];
    UIImageView *it1Imgb    = [[UIImageView alloc]init];
    NSString    *it1ImgName = [UserDefaults objectForKey:@"bottomBtm1"];
    
//    [it1Img sd_setImageWithURL:it1ImgName  placeholderImage:[UIImage imageNamed:img[0]]];
//    [it1Imgb sd_setImageWithURL:it1ImgName  placeholderImage:[UIImage imageNamed:img_selected[0]]];
    [it1Img sd_setImageWithURL:it1ImgName];
    [it1Imgb sd_setImageWithURL:it1ImgName];
    
    //        [it setFinishedSelectedImage:[UIImage imageNamed:img_selected[i]] withFinishedUnselectedImage:[UIImage imageNamed:img[i]]];
    it1.image =  [bottom1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    it1.selectedImage = [bottom1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [it1 setTitleTextAttributes:@{ NSForegroundColorAttributeName : SelectedColor } forState:UIControlStateNormal];
        
    
    //-------------------------------------------------------------------------------------------------------------
    UITabBarItem*it2=self.tabBar.items[1];
    
    offset.horizontal = 0.0;
    
    offset.vertical = -6.0;
    
    [it2 setTitlePositionAdjustment:offset];
    
    UIImageView *it2Img     = [[UIImageView alloc]init];
    UIImageView *it2Imgb    = [[UIImageView alloc]init];
    NSString    *it2ImgName = [UserDefaults objectForKey:@"bottomBtm2"];
    
//    [it2Img sd_setImageWithURL:it2ImgName  placeholderImage:[UIImage imageNamed:img[1]]];
//    [it2Imgb sd_setImageWithURL:it2ImgName  placeholderImage:[UIImage imageNamed:img_selected[1]]];
    [it2Img sd_setImageWithURL:it2ImgName];
    [it2Imgb sd_setImageWithURL:it2ImgName];
    
    //        [it setFinishedSelectedImage:[UIImage imageNamed:img_selected[i]] withFinishedUnselectedImage:[UIImage imageNamed:img[i]]];
    it2.image =  [bottom2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    it2.selectedImage = [bottom2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    AppDelegate*app2= APPDelegate;
    [it2 setMyAppCustomBadgeValue:app2.BadgeStr];
    [app2.BadgeArray addObject:it2];
    
    
    //-------------------------------------------------------------------------------------------------------------
    UITabBarItem*it3=self.tabBar.items[2];
    
    offset.horizontal = 0.0;
    
    offset.vertical = -6.0;
    
    [it3 setTitlePositionAdjustment:offset];
    
    UIImageView *it3Img     = [[UIImageView alloc]init];
    UIImageView *it3Imgb    = [[UIImageView alloc]init];
    NSString    *it3ImgName = [UserDefaults objectForKey:@"bottomBtm3"];
    
//    [it3Img sd_setImageWithURL:it3ImgName  placeholderImage:[UIImage imageNamed:img[2]]];
//    [it3Imgb sd_setImageWithURL:it3ImgName  placeholderImage:[UIImage imageNamed:img_selected[2]]];
    [it3Img sd_setImageWithURL:it3ImgName];
    [it3Imgb sd_setImageWithURL:it3ImgName];
    
    //        [it setFinishedSelectedImage:[UIImage imageNamed:img_selected[i]] withFinishedUnselectedImage:[UIImage imageNamed:img[i]]];
    it3.image =  [bottom3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    it3.selectedImage = [bottom3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    AppDelegate*app3= APPDelegate;
    [it3 setMyAppCustomBadgeValue:app3.BadgeStr];
    [app3.BadgeArray addObject:it3];
    
    
    //-------------------------------------------------------------------------------------------------------------
    UITabBarItem*it4=self.tabBar.items[3];
    
    offset.horizontal = 0.0;
    
    offset.vertical = -6.0;
    
    [it4 setTitlePositionAdjustment:offset];
    
    UIImageView *it4Img     = [[UIImageView alloc]init];
    UIImageView *it4Imgb    = [[UIImageView alloc]init];
    NSString    *it4ImgName = [UserDefaults objectForKey:@"bottomBtm4"];
    
//    [it4Img sd_setImageWithURL:it4ImgName  placeholderImage:[UIImage imageNamed:img[3]]];
//    [it4Imgb sd_setImageWithURL:it4ImgName  placeholderImage:[UIImage imageNamed:img_selected[3]]];
    [it4Img sd_setImageWithURL:it4ImgName];
    [it4Imgb sd_setImageWithURL:it4ImgName];
    
    //        [it setFinishedSelectedImage:[UIImage imageNamed:img_selected[i]] withFinishedUnselectedImage:[UIImage imageNamed:img[i]]];
    it4.image =  [bottom4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    it4.selectedImage = [bottom4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    AppDelegate*app4= APPDelegate;
    [it4 setMyAppCustomBadgeValue:app4.BadgeStr];
    [app4.BadgeArray addObject:it4];
    
}


- (IBAction)articleViewChanged:(id)sender {
    //UISegmentedControl *sc = (UISegmentedControl*)sender;
    [[NSNotificationCenter defaultCenter] postNotificationName:MAIN_TOP_VIEW_CHANGED object:sender userInfo:nil];
    //NSLog(@"Article View Changed:%d",sc.selectedSegmentIndex);
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
//    NSLog(@"Index:%d",item.tag);
    
//    [UIView animateWithDuration:0.3 animations:^{
//        float x=self.view.frame.size.width/tabBar.items.count*[tabBar.items indexOfObject:item];
//        label.frame=CGRectMake(x, -2, self.view.frame.size.width/tabBar.items.count, 2);
//
//    }];
    if (item.tag==0&&self.selectedIndex==0) {
        TableViewController*tab=(TableViewController*)[self.viewControllers[0]topViewController];
        [tab setIsMain:YES];
    }
    for (UITabBarItem*tem in tabBar.items) {
        [tem setTitleTextAttributes:@{ NSForegroundColorAttributeName : UnselectedColor } forState:UIControlStateNormal];
    }
   
    [item setTitleTextAttributes:@{ NSForegroundColorAttributeName :  SelectedColor} forState:UIControlStateNormal];
    
  
//    if(item.tag == 1){
//        [self.innerView setHidden:NO];
//    }else{
//        [self.innerView setHidden:YES];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
